﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BXH.Movie.Model;
using Dapper;

namespace BXH.Movie.Dal
{
    public class OrderInfoDal : BaseConnection
    {
        public void GetById()
        {
            //string sql = "select * from OrderInfo where Id=@Id";
            //Conn.Query<OrderInfoDto>();
            //            public int Id { get; set; }
            //public int UseId { get; set; }
            //public int MovieId { get; set; }
            //public string Seat { get; set; }
        }
        public bool Add(IEnumerable<OrderInfoInput> orderInfoInputs)
        {
            string sql = "insert into OrderInfo (UserId,SessionId,Seat)  values (@UserId,@SessionId,@Seat)";
            return Conn.Execute(sql, orderInfoInputs) > 0;
        }

        public IEnumerable<OrderInfoDto> GetOrderSeatByMovieId(int movieId)
        {
            string sql = @"select Seat from OrderInfo Where MovieId=@MovieId";
            return Conn.Query<OrderInfoDto>(sql, new { MovieId = movieId });
        }

        public bool Delete(int id)
        {
            string sql = @"delete from OrderInfo Where Id=@Id";
            return Conn.Execute(sql, new { Id =id })>0;
        }

        public IEnumerable<OrderInfoDto> GetOrderSeatBySessionId(int sessionId)
        {
            string sql = @"select Seat from OrderInfo  Where SessionId=@SessionId";
            return Conn.Query<OrderInfoDto>(sql, new { SessionId = sessionId });
        }

        public IEnumerable<OrderInfoDto> GetByUserId(int id)
        {
            string sql = @"select o.Id,u.UserCode,m.Name as MovieName,ms.BroadcastTime,o.Seat,m.TicketPrice  from OrderInfo as o
                                    inner join UserInfo as u on u.Id=o.UserId
									inner join MovieSession as ms on ms.Id=o.SessionId
                                    inner join MovieInfo as m on m.Id=ms.MovieId
                                    where o.UserId=@UserId";
            return Conn.Query<OrderInfoDto>(sql, new { UserId = id });
        }
    }
}
